﻿using System;
using WikiPlex.Formatting;

namespace WikiMaml.Renderers
{
    /// <summary>
    /// This will render the containment scopes.
    /// </summary>
    public class ContainerRenderer : IRenderer
    {
        /// <summary>
        /// Gets the id of a renderer.
        /// </summary>
        public string Id
        {
            get { return "Containers"; }
        }

        /// <summary>
        /// Determines if this renderer can expand the given scope name.
        /// </summary>
        /// <param name="scopeName">The scope name to check.</param>
        /// <returns>
        /// A boolean value indicating if the renderer can or cannot expand the macro.
        /// </returns>
        public bool CanExpand(string scopeName)
        {
            return (scopeName == MamlScopeName.IntroductionBegin
                    || scopeName == MamlScopeName.IntroductionWithOutlineBegin
                    || scopeName == MamlScopeName.IntroductionEnd
                    || scopeName == MamlScopeName.Markup
                    || scopeName == MamlScopeName.ParagraphBegin
                    || scopeName == MamlScopeName.ParagraphEnd
                    || scopeName == MamlScopeName.ProcedureBegin
                    || scopeName == MamlScopeName.ProcedureEnd
                    || scopeName == MamlScopeName.SectionBegin
                    || scopeName == MamlScopeName.SectionEnd
                    || scopeName == MamlScopeName.StepBegin
                    || scopeName == MamlScopeName.StepEnd);
        }

        /// <summary>
        /// Will expand the input into the appropriate content based on scope.
        /// </summary>
        /// <param name="scopeName">The scope name.</param><param name="input">The input to be expanded.</param><param name="htmlEncode">Function that will html encode the output.</param><param name="attributeEncode">Function that will html attribute encode the output.</param>
        /// <returns>
        /// The expanded content.
        /// </returns>
        public string Expand(string scopeName, string input, Func<string, string> htmlEncode, Func<string, string> attributeEncode)
        {
            switch (scopeName)
            {
                case MamlScopeName.IntroductionBegin:
                    return "<introduction>";
                case MamlScopeName.IntroductionWithOutlineBegin:
                    return "<introduction><autoOutline />";
                case MamlScopeName.IntroductionEnd:
                    return "</introduction>";
                case MamlScopeName.Markup:
                    return string.Format("<markup>{0}</markup>", input);
                case MamlScopeName.ParagraphBegin:
                    return "<para>";
                case MamlScopeName.ParagraphEnd:
                    return "</para>";
                case MamlScopeName.ProcedureBegin:
                    return string.Format("<procedure><title>{0}</title><steps class=\"ordered\">", htmlEncode(input));
                case MamlScopeName.ProcedureEnd:
                    return "</steps></procedure>";
                case MamlScopeName.SectionBegin:
                    return string.Format("<section><title>{0}</title><content>", htmlEncode(input));
                case MamlScopeName.SectionEnd:
                    return "</content></section>";
                case MamlScopeName.StepBegin:
                    return "<step><content>";
                case MamlScopeName.StepEnd:
                    return "</content></step>";
                default:
                    return input;
            }
        }
    }
}